# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2013 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------

class Cartasur::TopicsController < Cartasur::ApplicationController
  include Geokit::Geocoders

  def show
    @topic = Cartasur::Topic.find(params[:id])
    @forum = Cartasur::Forum.find(params[:forum_id])
    @postsminusreplies = Cartasur::Post.where("topic_id = ? AND reply_post_id IS NULL", @topic.id)
    @user = current_user

    render '/cartasur/forums/topics/show'
  end

  def new
    unless params[:forum_id].nil?
      @forum = Cartasur::Forum.find(params[:forum_id])
      @topic= Cartasur::Topic.new
      @post = Cartasur::Post.new
    render '/cartasur/forums/topics/new'
    end

  end

  def create
    @topic = Cartasur::Topic.new(:name => params[:cartasur_topic][:name], :last_poster_id => current_user.id, :last_post_at => Time.now, :forum_id => params[:cartasur_topic][:forum_id], :user_id => current_user.id)

    if @topic.save
        flash[:notice] = "Successfully created topic."
        redirect_to "/cartasur/forums/#{@topic.forum_id}"
      else
        redirect :action => 'new'
      end
  end

  def edit
    @topic  = Cartasur::Topic.find(params[:id])

  end
  def update
    @topic = Cartasur::Topic.find(params[:id])

    if @topic.update_attributes(params[:cartasur_topic])
      flash[:notice] = "The item was successfully updated!"
      redirect_to redirect_to "/forums/#{@topic.forum_id}"
    else
      flash[:alert] = "There was a problem when updating"
    end
  end

  def destroy
    topic = Cartasur::Topic.find(params[:id])
    if topic.destroy
      flash[:notice] = t 'cartasur.forums.topics.topicdeleted'
      redirect_to   Cartasur::Forum.find(params[:forum_id])
    end

  end
end

